package com.hn.controller;

import com.hn.pojo.User;
import com.hn.service.StudentService;
import com.hn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @作者 : wangchangdian
 * @创建时间 : 2019/3/22 - 9:25
 */
@Controller
@RequestMapping(value = "user")
public class UserContrller {
    @Autowired
    //@Resource
    private UserService userService;
    @Autowired
    private StudentService studentService;


    //查询用户
    @ResponseBody
    @RequestMapping(value = "findUser.html")
    public List<User> findUser(String name) {

        List<User> nameByUser = userService.findNameByUser(name);

        //返回对象，而不是string数据
        return nameByUser;
    }

    //删除用户
    @ResponseBody
    @RequestMapping(value = "deleteUser.html" /*,produces = "application/json;charset=utf-8"*/)
    public String deleteUser(HttpServletResponse response, String name, int id) {
        response.setHeader("content-Type", "text/html;charset=UTF-8");
        userService.deleteUserById(name, id);

        return "delete Success";

    }

    //增加用户
    @RequestMapping(value = "addUser.html")
    @ResponseBody
    public String addUser(int id, String address, int age, String name) {
        for (int i = 0; i <= 5; i++) {
            User user = new User();
            user.setId(id);
            user.setUserAddress(address);
            user.setUserAge(age);
            user.setUserName(name);
            userService.addUser(user);
        }

        return "add user success!";
    }

    //查询一个用户
    @ResponseBody
    @RequestMapping(value = "findOneUser.html")
    public User findOneUser(int id) {

        User oneByIdUser = userService.findOneByIdUser(id);

        //返回对象，而不是string数据
        return oneByIdUser;
    }

    //修改用户
    @ResponseBody
    @RequestMapping(value = "upUser.html")
    public String upUser(int id,String name,int age,String address) {
        User nameByUser = userService.findOneByIdUser(id);
        nameByUser.setUserName(name);
        nameByUser.setUserAge(age);
        nameByUser.setUserAddress(address);
        userService.updateUser(nameByUser);

        return "update success!";
    }


}
